import React from 'react'
import PropTypes from 'prop-types'

export default class extends React.PureComponent {

  static propTypes = {
    date: PropTypes.number
  }
  
  static defaultProps = {
    date: 0
  }

  componentWillMount() {
    this.setTime()
  }

  componentDidMount() {
    this.interval = setInterval(this.setTime, 1000)
  }

  componentWillUnmount() {
    clearInterval(this.interval)
  }

  setTime = () => {
    const date = new Date()
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDay()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const sec = date.getSeconds()

    this.setState(state => ({ ...state, time: `${year}-${month}-${day} ${hour}:${minute}:${sec}` }))
  }

  render() {
    const { time } = this.state
    return (
      <React.Fragment>
        {time}
      </React.Fragment>
    )
  }
}